 <!-- 添加简历页面 -->
<template>
  <div class="addResume">
    <h3 class="title">个人简历</h3>
    <el-form
      :model="ruleForm"
      status-icon
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
   
      <el-form-item label="求职类型" prop="type">
        <el-select v-model="ruleForm.type" placeholder="请选择">
          <el-option
            v-for="item in options1"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="求职岗位" prop="title">
        <el-input v-model="ruleForm.title"></el-input>
      </el-form-item>
      <el-form-item label="技能描述" prop="content">
        <el-input v-model="ruleForm.content"></el-input>
      </el-form-item>
      <el-form-item label="上传简历" prop="resume">
        <el-upload
          class="upload-demo"
          accept=".pdf,.doc,.docx"
          drag
          ref="upload"
          :data="data"
          :action="action"
          :headers="headers"
          :file-list="fileList"
          :auto-upload="true"
          :on-change="changeFile"
          :on-success="success"
          multiple
        >
          <i class="el-icon-upload"></i>
          <div class="el-upload__text">
            将文件拖到此处，或
            <em>点击上传</em>
          </div>
          <div class="el-upload__tip" slot="tip">
            重新上传,只能上传wrod文件
          </div>
        </el-upload>
         <el-button
                style="margin-top: 20px;width: 150px"
                size="mini"
                icon="el-icon-download"
                @click="downloadFile()"
                >下载简历</el-button
              >
      </el-form-item>
      <el-form-item>
        <el-button type="primary" :plain="true" @click="submitForm('ruleForm')"
          >保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import addResume from "../request/index";
import axios from "../request/axios";
import cookie from "js-cookie";
export default {
  data() {
    var type = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请选择岗位类型"));
      } else {
        return callback();
      }
    };
    var title = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("请填写岗位名称"));
      } else {
        return callback();
      }
    };
    var content = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("请填写您的技能描述"));
      } else {
        return callback();
      }
    };
  

    return {
      itemInfo: null,
      userinfo: null,
      username: "",
      data: {
        dataId: Math.random().toString(36).substr(3,6)+Date.now().toString(), //随机生成一个id
      },  //上传文件数据
      headers: {
        satoken: cookie.get("satoken"),
      }, //上传文件token
      action: axios.defaults.baseURL + "resume/upload", //上传地址
      fileList: [], //显示上传的图片列表
      selectedList: [],
      successList: [],

      ruleForm: {
        type: "", //岗位类型
        title: "", //岗位名称
        content: "", //技能描述
        resume: "", //简历
      },
      rules: {
        type: [{ validator: type, trigger: "blur" }],
        title: [{ validator: title, trigger: "blur" }],
        content: [{ validator: content, trigger: "blur" }],
      },

      // 企业类型
      options1: [
        {
          value: "IT",
          label: "IT",
        },
        {
          value: "医疗",
          label: "医疗",
        },
        {
          value: "金融",
          label: "金融",
        },
      ],
    };
  },

  methods: {
      //下载简历
    downloadFile(){
      if(this.itemInfo.fileInfo == null){
           this.$message.error("没有上传简历!");
      }else
      //下载地址
      window.location.href = axios.defaults.baseURL+"file/download/"+this.itemInfo.fileInfo.id;
    },

    changeFile(file, fileList) {
      //文件上传时触发 无论失败成功
      this.selectedList = fileList; //表示有选择的图片
    },

    success(res) {
      // console.log(this.successList);
        if(res.success){
            addResume.getResume({
        }).then((res) => {
            this.itemInfo = res.data.result
        })
         this.$message.success("上传成功!");
      }else{
         this.$message.error("上传失败!");
      }
    },
    // 添加简历信息
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const data  = {
            id: this.data.dataId,
            type: this.ruleForm.type,
            title: this.ruleForm.title,
            content: this.ruleForm.content,
          }
         console.log("data1"+JSON.stringify(data))
            addResume
              .addresume({
                id: data.id,
                type: data.type,
                title: data.title,
                content: data.content,
              })
              .then((res) => {
                console.log(res);
                if (res.data.success == true) {
                  this.$message.success("保存成功!");
                  this.resetForm();
                } else {
                  this.$message.error(res.data.message);
                }
              });
          
        } else {
          this.$message.error("请检查简历信息是否完整！");
          return false;
        }
      });
    },

   
  },

  created() {
    //是否存在简历处理
    addResume.getResume({
    }).then((res) => {
        //存在简历
           //存在简历
          if (res.data.result != null) {
            const result =  res.data.result
            this.itemInfo = result
            this.data.dataId = this.itemInfo.id
            this.ruleForm.type = this.itemInfo.type
            this.ruleForm.title =  this.itemInfo.title
            this.ruleForm.content =  this.itemInfo.content
          }//不存在简历
    })

    // 取出用户信息
    this.userinfo = JSON.parse(window.localStorage.getItem("userinfo"));
    if (this.userinfo) {
      this.username = this.userinfo.name;
    }
    console.log(this.userinfo);
  },
};
</script>

<style lang="scss" scoped >
* {
  padding: 0;
  margin: 0;
}
.addResume {
  ::v-deep .title {
    text-align: center;
    font-size: 20px;
  }
  ::v-deep .demo-ruleForm {
    width: 435px;
    margin: 0 auto;
  }
  ::v-deep .el-input__inner {
    height: 45px;
    font-size: 16px;
  }
  ::v-deep .el-form-item__label {
    font-size: 16px;
    position: relative;
    top: 15px;
  }
  ::v-deep .el-form-item__content {
    margin-top: 15px;
  }
  ::v-deep .el-form-item__error {
    font-size: 16px;
  }
  ::v-deep .el-button {
    width: 100px;
    height: 45px;
    font-size: 16px;
  }
  ::v-deep .el-select-dropdown__item {
    display: flex;
    justify-content: center;
  }
}
</style>
